//
//  MXFeedBackController.m
//  HNBaseProject
//
//  Created by xujie on 2024/6/4.
//

#import "MXFeedBackController.h"
#import "MxPublicNav.h"
#import "DCTextView.h"
@interface MXFeedBackController () <MxNavigationViewDelegate>
@property (nonatomic, strong) MxPublicNav *nav;
@property (nonatomic, strong) DCTextView *textV;
@property (nonatomic, copy) NSString *phoneNum;
@end

@implementation MXFeedBackController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#f5f5f5"];
    self.phoneNum = @"";
    [self createUI];
}

- (void)createUI {
    
    self.nav = [[MxPublicNav alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kNavBarHeight)];
    self.nav.titleLab.text = @"意见反馈";
    self.nav.delegate = self;
    [self.view addSubview:self.nav];
    
    MJWeakSelf
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(15, kNavBarHeight+20, kScreenW-30, 200)];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.layer.cornerRadius = 15;
    [self.view addSubview:bgView];
    
    DCTextView *textV = [[DCTextView alloc] init];
    textV.placeholder = @"等你很久了，你想和我们说点什么...";
    textV.placeholderColor = [UIColor colorWithHexString:@"#bebebe"];
    textV.textColor = [UIColor colorWithHexString:@"#333333"];
    textV.font = PFRFontMedium(16);
    [bgView addSubview:textV];
    [textV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.offset(10);
        make.right.offset(-10);
        make.bottom.offset(-45);
    }];
    
    self.textV = textV;
    
    UILabel *sureLab = [[UILabel alloc] init];
    [bgView addSubview:sureLab];
    sureLab.textAlignment = NSTextAlignmentCenter;
    sureLab.textColor = [UIColor whiteColor];
    sureLab.userInteractionEnabled = YES;
    sureLab.text = @"发布";
    sureLab.font = PFRFontMedium(12);
    sureLab.backgroundColor = [UIColor colorWithHexString:@"#5f71f5"];
    sureLab.layer.cornerRadius = 5;
    sureLab.layer.masksToBounds = YES;
    [sureLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.offset(-15);
        make.height.mas_equalTo(23);
        make.width.mas_equalTo(50);
    }];
    
    [sureLab addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        [weakSelf tapRelease];
    }]];
    
    UILabel *cancleLab = [[UILabel alloc] init];
    cancleLab.textColor = [UIColor colorWithHexString:@"#bebebe"];
    cancleLab.font =  PFRFontMedium(12);
    cancleLab.userInteractionEnabled = YES;
    cancleLab.text = @"取消";
    [bgView addSubview:cancleLab];
    [cancleLab addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        [weakSelf tapCancle];
    }]];
    [cancleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(sureLab.mas_left).offset(-15);
        make.centerY.mas_equalTo(sureLab );
    }];
    
    UIView *phoneBg = [[UIView alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(bgView.frame)+10, kScreenW-30, 60)];
    phoneBg.backgroundColor = [UIColor whiteColor];
    phoneBg.layer.cornerRadius = 15;
    [self.view addSubview:phoneBg];
    
    UILabel *phoneTitle = [[UILabel alloc] init];
    phoneTitle.text = @"电话号码";
    phoneTitle.font = PFRFontMedium(16);
    phoneTitle.textColor = [UIColor colorWithHexString:@"#000000"];
    [phoneBg addSubview:phoneTitle];
    [phoneTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.centerY.mas_equalTo(phoneBg);
    }];
    
    UITextField *nameField = [[UITextField alloc] init];
    nameField.textColor = [UIColor colorWithHexString:@"#000000"];
    nameField.placeholder = @"请填写";
    nameField.keyboardType = UIKeyboardTypeNumberPad;
    nameField.font = PFRFontMedium(16);
    [nameField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [phoneBg addSubview:nameField];
    [nameField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(85);
        make.right.offset(-15);
        make.top.bottom.offset(0);
    }];
    
}

- (void)textFieldDidChange:(UITextField *)textField {
    self.phoneNum = textField.text;
}

- (void)tapRelease {
    if (self.textV.text.length == 0) {
        [self.view xjmakeToast:@"请输入反馈内容"];
        return;
    }
    if (self.phoneNum.length == 0) {
        [self.view xjmakeToast:@"请填写电话号码"];
        return;
    }
    NSDictionary *param = @{@"adviceContent":self.textV.text,
                            @"contactPhone":self.phoneNum
    };

    MJWeakSelf
    [self.view makeToastActivity];
    [[DCHttpClient shareClient] requestWithPath:@"api/app/advice/addAdvice" params:param httpMethod:DCHttpRequestPost sucess:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        NSDictionary * dict = [responseObject mj_JSONObject];
        [weakSelf.view hideToastActivity];
        if (dict[DC_ResultCode_Key] && [dict[DC_ResultCode_Key] integerValue] == DC_Result_Success) {
            [weakSelf.view xjmakeToast:@"发布成功"];
            [weakSelf performSelector:@selector(leftTap) afterDelay:1.5];
        } else {

            [weakSelf.view xjmakeToast:dict[DC_ResultMsg_Key]];
        }
    } failture:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        [weakSelf.view hideToastActivity];
    }];
}

- (void)tapCancle {
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)leftTap {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self dc_navBarHidden:YES];
}


@end
